package org.dromara.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.ObjectUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.exam.domain.entity.ExamTagRel;
import org.dromara.exam.domain.bo.ExamTagRelBo;
import org.dromara.exam.domain.vo.ExamTagRelVo;
import org.dromara.exam.domain.dto.TemplateTagCount;
import org.dromara.exam.mapper.ExamTagRelMapper;
import org.dromara.exam.service.IExamTagRelService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签数据Service业务层处理
 *
 * @author xiyin321
 * @date 2024-04-17
 */
@RequiredArgsConstructor
@Service
public class ExamTagRelServiceImpl implements IExamTagRelService {

    private final ExamTagRelMapper baseMapper;

    /**
     * 查询标签分页数据列表
     */
    @Override
    public TableDataInfo<ExamTagRelVo> queryPageList(ExamTagRelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamTagRel> lqw = buildQueryWrapper(bo);
        Page<ExamTagRelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询标签数据列表
     */
    @Override
    public List<ExamTagRelVo> queryList(ExamTagRelBo bo) {
        LambdaQueryWrapper<ExamTagRel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExamTagRel> buildQueryWrapper(ExamTagRelBo bo) {
        LambdaQueryWrapper<ExamTagRel> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtils.isNotNull(bo.getRelId()), ExamTagRel::getRelId, bo.getRelId());
        lqw.eq(ObjectUtils.isNotNull(bo.getTagId()), ExamTagRel::getTagId, bo.getTagId());
        return lqw;
    }

    /**
     * 新增单条标签数据
     */
    @Override
    public Boolean insertByBo(ExamTagRelBo bo) {
        ExamTagRel add = MapstructUtils.convert(bo, ExamTagRel.class);
        validEntityBeforeSave();
        return baseMapper.insert(add) > 0;
    }

    /*
     * 批量插入标签数据
     * */
    @Override
    public Boolean batchInsert(Long relId, List<Long> tagIds) {
        // 准备要批量插入的数据列表
        List<ExamTagRel> entities = tagIds.stream()
            .map(tagId -> new ExamTagRel().setRelId(relId).setTagId(tagId))
            .collect(Collectors.toList());
        // 使用MyBatis Plus的批量插入功能
        return baseMapper.insertBatch(entities);
    }

    /**
     * 修改标签数据
     */
    @Override
    public Boolean updateByBo(ExamTagRelBo bo) {
        ExamTagRel update = MapstructUtils.convert(bo, ExamTagRel.class);
        validEntityBeforeSave();
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave() {
        //TODO 做一些数据校验,如唯一约束
    }

    /*根据id将标签数据删除*/
    @Override
    public Boolean deleteById(Long relId) {
        return baseMapper.deleteById(relId) > 0;
    }

    /**
     * 根据relId批量删除
     *
     * @param relIds relId列表
     */
    @Override
    public void batchDeleteByRelIds(Collection<Long> relIds) {
        QueryWrapper<ExamTagRel> wrapper = new QueryWrapper<>();
        wrapper.in("rel_id", relIds);
        baseMapper.delete(wrapper);
    }

    /**
     * 根据tagId批量删除
     *
     * @param tagIds tagId列表
     */
    @Override
    public void batchDeleteByTagIds(Collection<Long> tagIds) {
        QueryWrapper<ExamTagRel> wrapper = new QueryWrapper<>();
        wrapper.in("tag_id", tagIds);
        baseMapper.delete(wrapper);
    }


    /*查询题库标签统计*/
    @Override
    public List<TemplateTagCount> queryQuestionTagDataCount(String repoId) {
        return baseMapper.queryQuestionTagDataCount(repoId);
    }

    @Override
    public List<TemplateTagCount> queryTemplateTagCounts(List<Long> repoIds) {
        return baseMapper.queryTemplateTagCounts(repoIds);
    }
}
